StringinsertFragment Method
Syntax
Arguments
- fragmentstring
The fragment to insert.
- instancenumber
The zero base index of the location to insert the fragment.
- separatorstringregexparray
The string or regular expression to use as the boundary of the fragments. An array of two strings and/or regular expressions can be passed in to define a separate start and end boundary.
Returns
- valuestring
The new string with the fragment inserted.
Description
Extension to the native string variable to support insertion of fragments into a string.
Discussion
The String.insertFragment method can be used to insert fragments of text into a string.
The "instance" argument is the index of the location where the fragment is to be inserted.
The optional "separator" argument is the string(s) and/or regular expression(s) to use as the boundaries of the fragments. By default the separators are white-space and punctuation characters.
Example
var str = 'hello world (the earth)';
var str2 = str.insertFragment('goodbye',0);
// str2 = 'goodbye world (the earth)'
str2 = str.insertFragment('gia',0,['(',')']);
// str2 = 'hello world (gia)'